﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for IndustryInfoService
/// </summary>
public class IndustryInfoService: IIndustryInfoService
{
	public IndustryInfoService()
	{
	}
  
    public string getSymbol(string Industry_Name)
    {
        List<IndustryInfoItem> list = queryData(toSelectSymbolQuery(Industry_Name));
        if (list != null && list.Count > 0)
            return list[0].Symbol;
        return null;
    }
    public string getIndustryName(string Symbol)
    {
        List<IndustryInfoItem> list = queryData(toSelectIndustryNameQuery(Symbol));
        if (list != null && list.Count > 0)
            return list[0].Industry;
        return null;
    }

    public List<IndustryInfoItem> getIndustryInfoData()
    {
        List<IndustryInfoItem> list = queryData(toSelectAllDataQuery());
        return list;
    }
    private List<IndustryInfoItem> queryData(String query)
    {
        List<IndustryInfoItem> list = new List<IndustryInfoItem>();
        using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["StockDbConnection"].ToString()))
        {
            cn.Open();
            SqlCommand cmd = new SqlCommand(query, cn);
            SqlDataReader sqldatareader = cmd.ExecuteReader();
            while (sqldatareader.Read())
            {
                IndustryInfoItem a = new IndustryInfoItem();
                a.Symbol = sqldatareader.GetString(0);
                a.Industry = sqldatareader.GetString(1);
                a.Sector = sqldatareader.GetString(2);
                list.Add(a);
            }
            cn.Close();
        }
        return list;
    }
    public string toSelectSymbolQuery(string IndustryName)
    {
        return "Select * from IndustryInfo where Industry = '" + IndustryName + "'";
    }
    public string toSelectIndustryNameQuery(string Symbol)
    {
        return "Select * from IndustryInfo where Symbol = '" + Symbol + "'";
    }
    public string toSelectAllDataQuery()
    {
        return "Select * from IndustryInfo";
    }
}